<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../static/js/vue.js"></script>
    <link rel="stylesheet" href="../../static/css/element-ui.css">

    <script src="../../static/js/element-ui.js"></script>
    <script src="../../static/js/axios.js"></script>

</head>
<body>
<div id="app">
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="流通类型" prop="type">
                <el-input v-model="queryParams.type" placeholder="请选择流通类型">
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                >新增
                </el-button>
            </el-col>


        </el-row>

        <el-table v-loading="loading" :data="typeList">


            <el-table-column label="流通类型" align="center" prop="type"></el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改
                    </el-button>

                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
 style="float:right;margin-top: 20px"
                :page-sizes="[5, 10, 20, 50]"
                :page-size="5"
                v-show="total>0"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
        <!-- 添加或修改流通记录对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="流通类型" prop="type">
                    <el-input v-model="form.type" placeholder="请选择流通类型">
                    </el-input>

                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</div>
</body>


<script>

    new Vue({
        el: '#app',
        data: function () {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 流通记录表格数据
                typeList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    type: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {}
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询流通记录列表 */
            getList() {
                this.loading = true;
                axios({
                    url: '../../crud/type/list',
                    method: 'get',
                    params: this.queryParams
                }).then(response => {
                    this.typeList = response.data.rows;
                    this.total = response.data.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    type: null
                };

            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.queryParams = {
                    pageNum: 1,
                    pageSize: 10,
                    type: null
                },
                    this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加流通记录";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                axios({
                    url: '../../crud/type/' + id,
                    method: 'get'
                }).then(response => {
                    this.form = response.data.data;
                    this.open = true;
                    this.title = "修改流通类型";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            axios({
                                url: '../../crud/type',
                                method: 'put',
                                data: this.form
                            }).then(response => {
                                if (response.data.code == 200) {
                                    this.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });

                                    this.getList();
                                } else {

                                    this.$message.error(response.data.msg);
                                }
                                this.open = false;
                            });
                        } else {
                            axios({
                                url: '../../crud/type',
                                method: 'post',
                                data: this.form
                            }).then(response => {
                                if (response.data.code == 200) {
                                    this.$message({
                                        message: '新增成功',
                                        type: 'success'
                                    });

                                    this.getList();
                                } else {

                                    this.$message.error(response.data.msg);
                                }
                                this.open = false;
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除流通记录编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delType(ids);
                }).then(() => {
                    this.getList();
                    this.$message.error("删除成功");
                })
            },

            handleSizeChange(val) {
                this.queryParams.pageSize = val
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.pageNum = val;
                this.getList();
            }


        }
    });
</script>
